

#include "inapppurchasemanager.h"

#include "../sleipner_config.h"

#ifdef _PLATFORM_IOS
#include "c_inapppurchase.h"
#endif



static void     OnSuccessCB(void);
static void     OnFailCB(void);

InAppPurchaseManager&          InAppPurchaseManager::Instance()
{
    static InAppPurchaseManager	Inst;
	return Inst;
}


void		InAppPurchaseManager::LoadStore(const std::string& ProductName,
                                            InAppPurchaseHandler* pHandler
                                            )
{
    
    m_pHandler = pHandler;
    m_IsFinish = false;
    m_IsSuccess = false;
 
    
#ifdef _PLATFORM_IOS
    
    
    CLoadStore(
               ProductName.c_str(), 
               (OnPurchaseSuccess)OnSuccessCB, 
               (OnPurchaseFail)OnFailCB
               );
    
#endif
}

bool         InAppPurchaseManager::CanMakePurchases()
{
#ifdef _PLATFORM_IOS
    return CCanMakePurchases() > 0;
#endif
    return false;
}

void		InAppPurchaseManager::PurchaseProUpgrade(const std::string& ProdcutnName)
{
#ifdef _PLATFORM_IOS
    CPurchaseProUpgrade();
#endif    
}


bool		InAppPurchaseManager::IsFinishPurchasing()
{

    return m_IsFinish;
}

bool		InAppPurchaseManager::IsSuccessurchasing()
{

    return m_IsSuccess;
}


void        InAppPurchaseManager::OnSuccess()
{
    m_IsSuccess = true;
    m_IsFinish = true;
    
    if(m_pHandler)
        m_pHandler->OnPurchaseSuccess();
}


void        InAppPurchaseManager::OnFail()
{
    m_IsSuccess = false;
    m_IsFinish = true;
    
    if(m_pHandler)
        m_pHandler->OnPurchaseFail();
}

     
void        InAppPurchaseManager::OnCompleteLoadingStore()
{
   
}

 
void        InAppPurchaseManager::RestorePurchase()
{
#ifdef _PLATFORM_IOS
    CRestorePurchase();
#endif    
}


void     OnSuccessCB()
{
    InAppPurchaseManager::Instance().OnSuccess();
}


void     OnFailCB()
{
    InAppPurchaseManager::Instance().OnFail();
}



